//
//  LaunchVC.m
//  IkongWuProgram
//
//  Created by 徐益智 on 16/3/10.
//  Copyright © 2016年 Vdianjing. All rights reserved.
//

#import "LaunchVC.h"

#import "UserModel.h"
#import "MarkHeader.h"
#import "VDJConstant.h"
#import "UIHeader.h"
#import "ZUserDefault.h"
#import "AppDelegate.h"

@interface LaunchVC () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate>
{
    NSInteger _currentPage;
}

@property (nonatomic, strong)UICollectionView *collection;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) UIButton *enterButton;

@end

@implementation LaunchVC

- (UIButton *)enterButton
{
    if(!_enterButton){
        UIButton *button = [UIButton buttonWithFont:14 withTextColor:kBlackFontColor withBackgroundColor:kBackgroundColor withText:@"立即进入"];
        [button addTarget:self action:@selector(enterButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        _enterButton = button;
        [button setHidden:YES];
        button.layer.cornerRadius = 5;
        button.layer.masksToBounds = YES;
        
        button.sd_layout
        .leftSpaceToView(self.view, kMargin)
        .rightSpaceToView(self.view, kMargin)
        .bottomSpaceToView(self.view, kMargin)
        .heightIs(40);
    }
    return _enterButton;
}

- (UIPageControl *)pageControl
{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc] init];
        
        _pageControl.numberOfPages = 4;
        _pageControl.pageIndicatorTintColor = kWhiteColor;
        _pageControl.currentPageIndicatorTintColor = kLightYellowColor;
        _pageControl.currentPage = 0;
        _pageControl.enabled = NO;
    }
    return _pageControl;
}

- (UICollectionView *)collection
{
    if (!_collection) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.itemSize = CGSizeMake(kScreenWidth, kScreenHeight);
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        
        _collection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
        _collection.delegate = self;
        _collection.dataSource = self;
        _collection.pagingEnabled = YES;
        _collection.bounces = NO;
        _collection.showsHorizontalScrollIndicator = NO;
        _collection.showsVerticalScrollIndicator = NO;
        _collection.backgroundColor = kLightYellowColor;
    }
    return _collection;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.collection];
    self.collection.sd_layout.spaceToSuperView(UIEdgeInsetsMake(0, 0, 0, 0));
    
    [self.collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    // page
    [self.view addSubview:self.pageControl];
    self.pageControl.sd_layout
    .bottomSpaceToView(self.view, kMargin + 40)
    .leftSpaceToView (self.view, 50)
    .rightSpaceToView(self.view, 50)
    .heightIs(35);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"cell";
    
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"Launch_%ld", (long)indexPath.row + 1]]];
    
    
    return cell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _currentPage = scrollView.contentOffset.x / kScreenWidth;
    self.pageControl.currentPage = _currentPage;
    
    if (_currentPage == 3) {
        [self.enterButton setHidden:NO];
    }else {
        [self.enterButton setHidden:YES];
    }
}

- (void)enterButtonClick
{
    [ZUserDefault storageObject:kSymbol Key:kLaunchSymbol];
    
    AppDelegate *app = kAppdalegate;
    if ([UserModel queryModel]) {
        [app login];
    }else {
        [app notLogin];
    }
}

@end
